<template>
  <div class="withdraw-container">
    <el-card class="withdraw-card">
      <template #header>
        <div class="card-header">
          <span>账户提现</span>
        </div>
      </template>
      
      <el-form :model="withdrawForm" :rules="rules" ref="withdrawFormRef" label-width="100px">
        <el-form-item label="可提现金额">
          <span class="available-amount">¥ {{ availableAmount }}</span>
        </el-form-item>

        <el-form-item label="提现金额" prop="amount">
          <el-input-number 
            v-model="withdrawForm.amount" 
            :min="1" 
            :max="availableAmount"
            :precision="2"
            :step="100"
            placeholder="请输入提现金额"
          />
        </el-form-item>
        
        <el-form-item label="提现方式" prop="withdrawMethod">
          <el-select v-model="withdrawForm.withdrawMethod" placeholder="请选择提现方式">
            <el-option label="支付宝" value="alipay" />
            <el-option label="微信" value="wechat" />
            <el-option label="银行卡" value="bank" />
          </el-select>
        </el-form-item>

        <el-form-item label="收款账号" prop="account">
          <el-input v-model="withdrawForm.account" placeholder="请输入收款账号" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleWithdraw">确认提现</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const withdrawFormRef = ref(null)
const availableAmount = ref(10000.00) // 示例金额，实际应该从API获取

const withdrawForm = reactive({
  amount: 0,
  withdrawMethod: '',
  account: ''
})

const rules = {
  amount: [
    { required: true, message: '请输入提现金额', trigger: 'blur' },
    { type: 'number', min: 1, message: '提现金额必须大于0', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value > availableAmount.value) {
          callback(new Error('提现金额不能大于可提现金额'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  withdrawMethod: [
    { required: true, message: '请选择提现方式', trigger: 'change' }
  ],
  account: [
    { required: true, message: '请输入收款账号', trigger: 'blur' }
  ]
}

const handleWithdraw = async () => {
  if (!withdrawFormRef.value) return
  
  await withdrawFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提现逻辑
      ElMessage.success('提现申请已提交')
    } else {
      ElMessage.error('请检查输入信息')
      return false
    }
  })
}
</script>

<style scoped>
.withdraw-container {
  padding: 20px;
}

.withdraw-card {
  max-width: 600px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.available-amount {
  font-size: 20px;
  color: #409EFF;
  font-weight: bold;
}
</style> 